package com.hrbxlh.action;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hrbxlh.actionImpl.CourseActionImpl;
import com.hrbxlh.actionImpl.ParamActionImpl;


/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：ParamAction.java
* Description：参数管理action类
* History：
* Date           Author               Desc
* 2017.4.17		  xzx                 建立
* -------------------------------------------------------
*/

@Controller
public class ParamAction {

	@Autowired
	@Qualifier("paramActionImpl")
	private ParamActionImpl paramActionImpl;
	
	@Autowired
	@Qualifier("courseActionImpl")
	private CourseActionImpl courseActionImpl;
	
	/**
	 * @description 进入参数管理页面
	 * @param request
	 * @param response
	 * @return  跳转到参数管理页面
	 */
	@RequestMapping(value = "/skipParamMana", method = RequestMethod.POST)
	public void skipParamMana(HttpServletResponse response)   {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ParamAction.class.getName());
		try {
			resultMap = paramActionImpl.getParamDetail();
			resultMap.put("result", "success");
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace();
			logger.error("跳转参数管理页面出现异常skipParamMana", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @description 保存参数
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveParam", method = RequestMethod.POST)
	public void  saveParam (HttpServletResponse response , String jsonStr)   {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ParamAction.class.getName());
		try { 
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//保存参数
			boolean result = paramActionImpl.saveParam(paramMap);
			//返回参数
			String resultMark = (result) ? "success" : "fail";
			resultMap.put("result", resultMark);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("保存参数出现异常saveParam", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
}